package com.imooc.miaosha.service.impl;

import com.google.common.collect.Lists;
import com.imooc.miaosha.common.Const;
import com.imooc.miaosha.dao.*;

import com.imooc.miaosha.dto.OrderDetailDTO;
import com.imooc.miaosha.pojo.*;
import com.imooc.miaosha.service.OrderService;


import com.imooc.miaosha.vo.OrderShippingVO;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    MiaoshaOrderMapper miaoshaOrderMapper;
    @Autowired
    OrderInfoMapper orderInfoMapper;
    @Autowired
    MiaoshaGoodsMapper miaoshaGoodsMapper;
    @Autowired
    ShippingMapper shippingMapper;
    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public List<OrderShippingVO> list(Long userId) {
        List<OrderShippingVO> orderShippingVOList = Lists.newArrayList();
        List<MiaoshaOrder> miaoshaOrderList = miaoshaOrderMapper.getMiaoshaOrderListByUserId(userId);
        if (miaoshaOrderList.size() == 0) {
            return null;
        }
        for (MiaoshaOrder miaoshaOrder : miaoshaOrderList) {
            OrderInfo orderInfo = orderInfoMapper.selectByPrimaryKey(miaoshaOrder.getOrderId());
            MiaoshaGoods miaoshaGoods = miaoshaGoodsMapper.selectByPrimaryKey(miaoshaOrder.getSecondKillId());
            Shipping shipping = shippingMapper.selectByPrimaryKey(orderInfo.getShippingId());
            Goods goods = goodsMapper.selectByPrimaryKey(miaoshaGoods.getGoodsId());
            OrderShippingVO orderShippingVO = OrderShippingVO.builder()
                    .orderId(orderInfo.getId())
                    .count(orderInfo.getGoodsCount())
                    .createTime(orderInfo.getCreateTime())
                    .imgUrl(goods.getGoodsImg())
                    .payChannel(orderInfo.getOrderChannel())
                    .productName(orderInfo.getGoodsName())
                    .secondKillPrice(miaoshaGoods.getMiaoshaPrice())
                    .status(orderInfo.getStatus())
                    .shipping(shipping)
                    .build();
            orderShippingVOList.add(orderShippingVO);
        }
        return orderShippingVOList;
    }

    @Override
    public OrderDetailDTO detail(Long orderId, Long userId) {
        MiaoshaOrder miaoshaOrder = miaoshaOrderMapper.getMiaosShaOrderByUserIdAndOrderId(userId, orderId);
        OrderInfo orderInfo = orderInfoMapper.selectByPrimaryKey(miaoshaOrder.getOrderId());
        MiaoshaGoods miaoshaGoods = miaoshaGoodsMapper.selectByPrimaryKey(miaoshaOrder.getSecondKillId());
        Shipping shipping = shippingMapper.selectByPrimaryKey(orderInfo.getShippingId());
        return OrderDetailDTO.builder()
                .orderId(orderId)
                .count(orderInfo.getGoodsCount())
                .createTime(orderInfo.getCreateTime())
                .imgUrl("")
                .payChannel(orderInfo.getOrderChannel())
                .productName(orderInfo.getGoodsName())
                .secondKillPrice(miaoshaGoods.getMiaoshaPrice())
                //todo 暂定
                .shipping(shipping)
                .status(orderInfo.getStatus())
                .build();
    }


    @Override
    public OrderShippingVO getOrder(Long miaoshaId, Long userId) {
        MiaoshaOrder miaoshaOrder = miaoshaOrderMapper.getOrderInfoByUserIdAndMiaoshaId(miaoshaId, userId);
        OrderInfo orderInfo = orderInfoMapper.selectByPrimaryKey(miaoshaOrder.getOrderId());
        MiaoshaGoods miaoshaGoods = miaoshaGoodsMapper.selectByPrimaryKey(miaoshaId);
        return OrderShippingVO
                .builder()
                .status(orderInfo.getStatus())
                .count(orderInfo.getGoodsCount())
                .createTime(orderInfo.getCreateTime())
                // TODO: 18-10-28 imgUrl
                .imgUrl(null)
                .orderId(orderInfo.getId())
                .payChannel(orderInfo.getOrderChannel())
                .productName(orderInfo.getGoodsName())
                .secondKillPrice(miaoshaGoods.getMiaoshaPrice())
                .build();

    }

    @Override
    public void closeOrder() {
        List<OrderInfo> orderInfoList = orderInfoMapper.getOrderInfoListByCreateTime(Const.ORDER_STATUS.NO_PAY, DateUtils.addHours(new Date(), Const.CLOSE_ORDER_HOUR));
        for (OrderInfo orderInfo : orderInfoList) {
            Integer goodsStock = goodsMapper.getGoodsStock(orderInfo.getGoodsId());
            if (goodsStock != null) {
                goodsStock += orderInfo.getGoodsCount();
                Goods goods = new Goods();
                goods.setId(orderInfo.getGoodsId());
                goods.setGoodsStock(goodsStock);
                goodsMapper.updateByPrimaryKeySelective(goods);
            }
            orderInfo.setStatus(Const.ORDER_STATUS.HAVE_TIMED);
            orderInfoMapper.updateByPrimaryKeySelective(orderInfo);
        }
    }
}
